//
//  YJTabBarController.m
//  YJMiGao
//
//  Created by 张龙 on 16/11/4.
//  Copyright © 2016年 张龙. All rights reserved.
//

#import "YJTabBarController.h"
#import "YJNavigationController.h"
#import "YJTabBar.h"
#import "HomeViewController.h"
#import "FriendCircleViewController.h"
#import "ClassRoomViewController.h"
#import "MyCenterViewController.h"
#import "NOLoginViewController.h"

@interface YJTabBarController ()<UITabBarDelegate>

@end

@implementation YJTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 添加子控制器
    [self addChildVc:[[HomeViewController alloc] init] title:@"首页" image:@"home_def" selectedImage:@"home_sel"];
    [self addChildVc:strIsEmpty([Global getUserTokenInfo])?[[NOLoginViewController alloc]initWithIndex:1]: [[JHUserInfo getUserInfo].role_type isEqualToString:@"6"] ?[[NOLoginViewController alloc]initWithIndex:4] : [[FriendCircleViewController alloc] init] title:@"校园圈" image:@"circle_def" selectedImage:@"circle_sel"];
    [self addChildVc:strIsEmpty([Global getUserTokenInfo])?[[NOLoginViewController alloc]initWithIndex:2]:  [[JHUserInfo getUserInfo].role_type isEqualToString:@"6"] ?[[NOLoginViewController alloc]initWithIndex:4] : [[ClassRoomViewController alloc] init] title:@"课堂" image:@"classroom_def" selectedImage:@"classroom_sel"];
    [self addChildVc:strIsEmpty([Global getUserTokenInfo])?[[NOLoginViewController alloc]initWithIndex:3]:[[MyCenterViewController alloc] init] title:@"我的" image:@"my_def" selectedImage:@"my_sel"];

    UITabBar *tabBar = [[UITabBar alloc] init];
    tabBar.delegate = self;
    // KVC：如果要修系统的某些属性，但被设为readOnly，就是用KVC，即setValue：forKey：。
    [self setValue:tabBar forKey:@"tabBar"];
}

/**
 *  添加一个子控制器
 *
 *  @param childVc       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字(可以设置tabBar和navigationBar的文字)
    childVc.title = title;
    // 设置子控制器的tabBarItem图片
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    // 禁用图片渲染
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    // 设置文字的样式
    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : KBACKNAVIGATIONCOLOR} forState:UIControlStateSelected];
    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : KTEXTMAINCOLOR} forState:UIControlStateNormal];
//    childVc.view.backgroundColor = KBACKGROUNDCOLOR; // 这句代码会自动加载主页，消息，发现，我四个控制器的view，但是view要在我们用的时候去提前加载


    // 为子控制器包装导航控制器
    YJNavigationController *navigationVc = [[YJNavigationController alloc] initWithRootViewController:childVc];
    // 添加子控制器
    [self addChildViewController:navigationVc];
}

#pragma ZTTabBarDelegate
///**
// *  加号按钮点击
// */
//- (void)tabBarDidClickPlusButton:(YJTabBar *)tabBar
//{
//    UIViewController *vc = [[UIViewController alloc] init];
//    [self presentViewController:vc animated:YES completion:nil];
//}
//
//- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
//{
//    MyLog(@"%@,%@",tabBar,item);
//}

@end
